// pages/advcanment/advcanment.js
const app = getApp();
var common = require('../../utils/util.js');
Page({
    data: {
        week: [
            {
                times: "周一",
                key: 1,
                isshow: false
            },
            {
                times: "周二",
                key: 2,
                isshow: false
            },
            {
                times: "周三",
                key: 3,
                isshow: false
            },
            {
                times: "周四",
                key: 4,
                isshow: false
            },
            {
                times: "周五",
                key: 5,
                isshow: false
            },
            {
                times: "周六",
                key: 6,
                isshow: false
            },
            {
                times: "周日",
                key: 0,
                isshow: false
            }
        ],
        starttime: '',
        startindex: '',
        endtiem: '',
        endindex: '',
        ismult: false,
        startArr: [],//开始时间
        endArr: [],//结束时间
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.setNavigationBarTitle({
            title: '预约设置'
        })
        var weekArr = this.data.week;
        app.http.POST("/MiniCardReservaSet/GetMyReservaSet", {}, (err, data) => {
            if (data.code == 1) {
                var timeTable = data.data.TimeTable;
                var timeArry = timeTable.split(',');
                for (var i = 0; i < timeArry.length; i++) {
                    weekArr.forEach(function (obj, j) {
                        if (obj.key == parseInt(timeArry[i])) {
                            obj.isshow = true;
                        }
                    })
                }
                this.setData({
                    "week": weekArr,
                    "starttime": common.formatDate(data.data.BTime, 'hh:mm'),
                    "endtiem": common.formatDate(data.data.ETime, 'hh:mm'),
                    "ismult": data.data.IsMultiple
                });
                var _this = this;
                var starttime = _this.data.starttime;
                var endtiem = _this.data.endtiem;
                var endlist = _this.data.endArr
                var startlist = _this.data.startArr;
                var endlist = _this.data.endArr
                startlist.forEach(function (obj, i) {
                    if (starttime) {
                        if (starttime == obj) {
                            _this.setData({
                                startindex: i
                            })
                        }
                    }
                });
                endlist.forEach(function (obj, i) {
                    if (endtiem) {
                        if (endtiem == obj) {
                            _this.setData({
                                endindex: i
                            })
                        }
                    }
                })
            } else {
                wx.showToast({
                    title: '请求失败',
                    icon: 'none',
                    duration: 2000
                })
            }
        });
        var timeArr = [];
        //循环时间数据
        var padLeft = function (str, len, charStr) {
            if (str.length >= len) return str;
            for (var i = str.length; i < len; i++) {
                str = charStr + str;
            }
            return str;
        }
        for (var i = 0; i < 24; i++) {
            var timeStr = padLeft(i.toString(), 2, '0');
            timeArr.push(timeStr + ':00');
            timeArr.push(timeStr + ':30');
        }

        this.setData({
            "startArr": timeArr,
            "endArr": timeArr
        })
        console.log('timeArr', timeArr);
    },
    //点击添加周期
    addtive: function (e) {
        var index = e.currentTarget.dataset.index;
        var arr = this.data.week;
        if (arr[index].isshow) {
            arr[index].isshow = false;
        } else {
            arr[index].isshow = true;
        }
        this.setData({
            "week": arr
        })
    },
    //时间选择器
    bindTimeChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        var _this = this;
        var index = e.detail.value
        var startlist = _this.data.startArr
        startlist.forEach(function (obj, i) {
            if (index == i) {
                _this.setData({
                    starttime: obj
                })
            }
        });
    },
    bindTimeChangetwo: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        var _this = this;
        var index = e.detail.value
        var endlist = _this.data.endArr
        endlist.forEach(function (obj, i) {
            if (index == i) {
                _this.setData({
                    endtiem: obj,
                    endindex: index
                })
            }
        });
    },
    switchChange: function (e) {
        this.setData({
            ismult: e.detail.value
        })
    },
    //提交数据
    formSubmit: function (e) {
        var week = this.data.week;
        var timeTable = "";
        for (var i = 0; i < week.length; i++) {
            if (week[i].isshow) {
                timeTable += week[i].key + ",";
            }
        }
        timeTable = timeTable == "" ? "" : timeTable.substring(0, timeTable.length - 1);
        var param = {
            TimeTable: timeTable,
            BTime: this.data.starttime,
            ETime: this.data.endtiem,
            IsMultiple: this.data.ismult ? 1 : 0,
        }
        app.http.POST('/MiniCardReservaSet/UpdateMyReservaSet', param, (err, data) => {
            if (data.code == 1) {
                wx.showToast({
                    title: '保存成功',
                    icon: 'success',
                    duration: 2000
                })
            } else {
                wx.showToast({
                    title: '请求失败',
                    icon: 'none',
                    duration: 2000
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        var userInfo = app.globalData.userInfo;
        //判断当前用户是否开通服务,开通则分享美名片页,否则分享访问记录页
        if (userInfo == null || typeof (userInfo) == "undefined") {
            app.getUserall(function (res) {
                userInfo = res;
            });
        }
        var path = userInfo.UserType == 2 ? "/pages/index/index?cid=" + userInfo.Id : "/pages/cardrecord/cardrecord";
        return {
            title: userInfo.NickName + "的美名片",
            path: path,
            success: function (res) {
                wx.showToast({
                    title: '分享成功',
                    icon: "success",
                    duration: 200
                })
            }
        }
    }
})